﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using Microsoft.Windows.Controls.Ribbon;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data.SqlClient;
using System.Data;

namespace ScrumMagic
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class MainWindow : RibbonWindow
    {
        public MainWindow()
        {
            InitializeComponent();
            FormController.MainForm = this;
            RibbonTabController.MainForm = this;
        }

        //string myId1 = myId;
        public ProductListForm FormToOpenProductListWind;
        public ProductListForm FormToOpenProductListTab;

        #region Main
        public TabControl MainTabControl
        {
            get { return FormTabControl; }
        }

        private void Window_Close(object sender, EventArgs e)
        {
            //save windows in array. Foreach gets error: Collection was modified; enumeration operation may not execute.
            //ArrayList ContainerListValues = new ArrayList();

            //foreach (ICommonContainer CurrentItem in FormController.FormList.Values)
            //{
            //    if (CurrentItem.Type == FormController.ContainerTypes.window) ContainerListValues.Add(CurrentItem);
            //}
            ArrayList ContainerListValues = new ArrayList(FormController.FormList.Values);




            foreach (ICommonContainer CurrentItem in FormController.FormList.Values)
            {
                if (CurrentItem != null) CurrentItem.CloseContainer();
            }
            
            //dont know how to close CommonForm correctly.
            Environment.Exit(0);

        }

        private void expanderActivity_Expanded(object sender, RoutedEventArgs e)
        {

        }

        private void FormTabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        #endregion

        #region Buttons

        // по кнопке Задания показываем список заданий, используя контроллер
        private void OnMyTasksClick(object sender, RoutedEventArgs e)
        {
            CommonForm FormToOpen = new TaskListForm();
            FormToOpen.Title = "Task List";
            FormController.OpenForm(FormToOpen, FormController.ContainerTypes.tab);

            // show contextual menu
            //RibbonTabController.ActivateTab(FormToOpen.RibbonContextualTab);
        }

        private void OnMyTasksWindowClick(object sender, RoutedEventArgs e)
        {
            // пример вызова карточки в отдельном окне
            CommonForm FormToOpen = new TaskListForm();
            FormToOpen.Title = "Task List";
            FormController.OpenForm(FormToOpen, FormController.ContainerTypes.window);
        }

        // по кнопке All Users показываем список всех пользователей
        private void btnAllUsers_Click(object sender, RoutedEventArgs e)
        {
            CommonForm FormToOpen = new AllUsersList();
            FormToOpen.Title = "All Users";
            FormController.OpenForm(FormToOpen, FormController.ContainerTypes.tab);
        }

        // по кнопке Продукты показываем список продуктов, используя контроллер
        private void OnMyProductClick(object sender, RoutedEventArgs e)
        {
            CommonForm FormToOpenProductListTab = new ProductListForm();
            FormToOpenProductListTab.Title = "Product List";
            FormController.OpenForm(FormToOpenProductListTab, FormController.ContainerTypes.tab);
        }
        private void OnMyProductWindowClick(object sender, RoutedEventArgs e)
        {
            FormToOpenProductListWind = new ProductListForm();
            FormToOpenProductListWind.Title = "Product List";
            FormController.OpenForm(FormToOpenProductListWind, FormController.ContainerTypes.window);
        }

        #endregion

        #region Ribbon
        private void OnRibbonCommandCanExecute(object sender, System.Windows.Input.CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
        }

        private void OnOpenedWindowsClick(object sender, MouseButtonEventArgs e)
        {
            //return;
            OpenedWindows.Items.Clear();

            bool isActive = false;
            foreach (string CurrentItem in FormController.FormList.Keys)
            {
                ICommonContainer Container = (ICommonContainer)FormController.FormList[CurrentItem];
                if (Container.Type == FormController.ContainerTypes.tab)
                {
                    TabItem tabContainer = (TabItem)FormController.FormList[CurrentItem];
                    isActive = tabContainer.IsSelected;
                }
                RibbonMenuItem menuItem = new RibbonMenuItem();
                menuItem.Header = CurrentItem;
                menuItem.IsCheckable = true;
                menuItem.IsChecked = isActive;
                menuItem.Click += new RoutedEventHandler(OnMenuItemClick);
                menuItem.Checked += new RoutedEventHandler(OnMenuItemChecked);
                menuItem.Tag = Container;
                OpenedWindows.Items.Add(menuItem);
            }
        }

        void OnMenuItemClick(object sender, RoutedEventArgs e)
        {
            RibbonMenuItem menuItem = (RibbonMenuItem)sender;
            menuItem.IsChecked = !menuItem.IsChecked;
        }

        void OnMenuItemChecked(object sender, RoutedEventArgs e)
        {
            RibbonMenuItem menuItem = (RibbonMenuItem)sender;
            ICommonContainer Container = (ICommonContainer)menuItem.Tag;
            FormController.OpenForm(Container.CommonForm, Container.Type);
        }

        #endregion

        
        private void btnMyProfile_Click(object sender, RoutedEventArgs e)
        {
            CommonForm MyProfile = new UserCardForm(CurrentUser.Instance.Id);
            MyProfile.Title = "My Profile";
            FormController.OpenForm(MyProfile, FormController.ContainerTypes.tab);
        }

        private void btnMySprints_Click(object sender, RoutedEventArgs e)
        {
            Window_Close(sender, e);
            CommonForm sprintListForm = new SprintListForm();
            sprintListForm.Title = "My Sprints";
            FormController.OpenForm(sprintListForm, FormController.ContainerTypes.tab);
        }

        private void btnMyProjects_Click(object sender, RoutedEventArgs e)
        {
            CommonForm projListForm = new ProjectListForm();
            projListForm.Title = "My Projects";
            FormController.OpenForm(projListForm, FormController.ContainerTypes.tab);
        }


    }
}
